package com.jbeer.framework.web.viewrender;

import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import com.jbeer.framework.JBeer;
import com.jbeer.framework.exception.RenderingViewException;
import com.jbeer.framework.web.ModelAndView;

/**
 * 
 * @author bieber
 * @version $Id: JSONRender.java, v 0.1 2014年4月24日 下午5:10:38 bieber Exp $
 */
public class JSONRender {

    public static void render(HttpServletRequest request,HttpServletResponse response,ModelAndView modelView) throws RenderingViewException{
        try{
            ObjectMapper mapper = new ObjectMapper();
            if(modelView.getData()==null){
                throw new RenderingViewException("未设置返回前端的数据，请设置相关数据....");
            }
            String retMsg = mapper.writeValueAsString(modelView.getData());  
            response.setContentType("Content-Type:application/json;charset="+JBeer.applicationEncode);
            Writer writer = response.getWriter();
            writer.write(retMsg);
            writer.flush();
            writer.close();
        }catch(Exception e){
            throw new RenderingViewException(e);
        }
        }
    
    
}
